package org.geektimes.projects.user.web.controller;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.service.UserService;
import org.geektimes.projects.user.service.UserServiceImpl;
import org.geektimes.web.mvc.controller.PageController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

@Path("/user")
public class UserController implements PageController {

    private UserService userService = new UserServiceImpl();

    private AtomicInteger atomicLong = new AtomicInteger(0);

    @POST
    @Path("/signup") // /hello/world -> HelloWorldController
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Throwable {

        try {
            User user = new User();
            user.setPassword(request.getParameter("password"));
            user.setName(request.getParameter("username"));
            user.setId(atomicLong.getAndIncrement());
            if (userService.register(user)) {
                System.out.println("11111");
                return "succes.jsp";
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return "error.jsp";

    }
}
